function calQty(qtyMap, errorMap) {
    if (qtyMap.isEmpty()) {
        return false;
    }
    var keys = qtyMap.keys();
    for (var key = 0; key < keys.length; key++) {
        var qty = keys[key];
        var qtyRetail = qtyMap.get(qty);
        for (var key2 = 0; key2 < keys.length; key2++) {
            var qty2 = keys[key2];
            var qty2Retail = qtyMap.get(qty2);
            if (qty !== qty2 && calQtyRetail(qtyRetail, qty, qty2Retail, qty2, errorMap)) {
                return true;
            }
        }
    }
}

function calQtyRetail(qtyList, qty, qtyList2, qty2, errorMap) {
    for (var lis = 0; lis < qtyList.length; lis++) {
        var qtyO = qtyList[lis];
        var qtyRetail = parseFloat((qtyO.RETAIL / qty).toFixed(2));

        for (var lis2 = 0; lis2 < qtyList2.length; lis2++) {
            var qtyO2 = qtyList2[lis2];
            var qty2Retail = parseFloat((qtyO2.RETAIL / qty2).toFixed(2));
            if (overlaps(qtyO.EFFECTIVE_DATE, qtyO.EXPIRATION_DATE, qtyO2.EFFECTIVE_DATE, qtyO2.EXPIRATION_DATE)) {
                //如果小包装的单价比大包装的单价高，则报错
                if (qty < qty2 && qtyRetail < qty2Retail) {
                    errorMap.put(qty, qtyO);
                    errorMap.put(qty2, qtyO2);
                    // console.log("" + qty + ":" + qtyRetail + "-" + qty2 + ":" + qty2Retail)
                    return true;
                }
            }
        }
    }
}

function cpkPriceStep(itemSpilt, CSRL, cpkList, errorMap) {
    for (var mi = 0; mi < itemSpilt.length; mi++) {
        var first = itemSpilt[mi];
        var firQty = cpkList.keysByValue(first.ITEM_NBR)[0];
        var newList = new Map();
        for (var sc = 0; sc < itemSpilt.length; sc++) {
            var second = itemSpilt[sc];
            var secQty = cpkList.keysByValue(second.ITEM_NBR)[0];
            if (firQty !== secQty && first.ITEM_NBR !== second.ITEM_NBR) {
                if (!isNull(first.SAME_ID) ){
                    if (first.SAME_ID === second.ID){
                        var firNulList2 = [];
                        var secNulList2 = [];
                        firNulList2.push(first);
                        secNulList2.push(second);
                        newList.put(firQty, firNulList2);
                        newList.put(secQty, secNulList2);
                        if (calQty(newList, errorMap)) {
                            return true;
                        } else {
                            newList.clear();
                        }
                    }
                }else {
                    //如果有一方为CSRL数据则自己算
                    if (isNull(first.EFFECTIVE_DATE) || isNull(second.EFFECTIVE_DATE)) {
                        if (isNull(first.EFFECTIVE_DATE) && isNull(second.EFFECTIVE_DATE)) {
                            continue;
                        }
                        var firNulList = [];
                        var secNulList = [];
                        if (isNotEmpty(first.EFFECTIVE_DATE)) {
                            var sCopy = deepCopy(first);
                            sCopy.ITEM_NBR = second.ITEM_NBR;
                            var sCsrl = filterCsrl(CSRL, second.ITEM_NBR, first.PRICE_TRAIT, first.EFFECTIVE_DATE, first.EXPIRATION_DATE);
                            if (isNotEmpty(sCsrl)) {
                                sCsrl.sort(function (a, b) {
                                    return a.CUST_BASE_RTL_AMT > b.CUST_BASE_RTL_AMT ? 1 : -1;
                                });
                                sCopy.RETAIL = sCsrl[0].CUST_BASE_RTL_AMT;
                            } else {
                                sCopy.RETAIL = itemList.get(secQty).RETAIL
                            }
                            sCopy.TYPE = 'csrl';
                            firNulList.push(first);
                            secNulList.push(sCopy);
                        }
                        if (isNotEmpty(second.EFFECTIVE_DATE)) {
                            var fCopy = deepCopy(second);
                            fCopy.ITEM_NBR = first.ITEM_NBR;
                            var sCsrl2 = filterCsrl(CSRL, first.ITEM_NBR, second.PRICE_TRAIT, second.EFFECTIVE_DATE, second.EXPIRATION_DATE);
                            if (isNotEmpty(sCsrl)) {
                                sCsrl2.sort(function (a, b) {
                                    return a.CUST_BASE_RTL_AMT > b.CUST_BASE_RTL_AMT ? 1 : -1;
                                });
                                fCopy.RETAIL = sCsrl2[0].CUST_BASE_RTL_AMT;
                            } else {
                                fCopy.RETAIL = itemList.get(firQty).RETAIL
                            }
                            fCopy.TYPE = 'csrl';
                            firNulList.push(fCopy);
                            secNulList.push(second);
                        }
                        newList.put(firQty, firNulList);
                        newList.put(secQty, secNulList);
                        if (calQty(newList, errorMap)) {
                            return true;
                        } else {
                            newList.clear();
                        }
                    }
                    else {
                        if (overlaps(first.EFFECTIVE_DATE, first.EXPIRATION_DATE, second.EFFECTIVE_DATE, second.EXPIRATION_DATE)) {
                            var secondList = [];
                            var firstList = [];
                            if (dateCompare(first.EFFECTIVE_DATE, second.EFFECTIVE_DATE) > 0) {
                                var deSec = deepCopy(second);
                                deSec.EFFECTIVE_DATE = first.EFFECTIVE_DATE;
                                deSec.EXPIRATION_DATE = second.EFFECTIVE_DATE;
                                var findCsrl1 = filterCsrl(CSRL, deSec.ITEM_NBR, deSec.PRICE_TRAIT, deSec.EFFECTIVE_DATE, deSec.EXPIRATION_DATE);
                                if (isNotEmpty(findCsrl1)) {
                                    findCsrl1.sort(function (a, b) {
                                        return a.CUST_BASE_RTL_AMT > b.CUST_BASE_RTL_AMT ? 1 : -1;
                                    });
                                    deSec.RETAIL = findCsrl1[0].CUST_BASE_RTL_AMT;
                                    deSec.TYPE = 'csrl';
                                }
                                secondList.push(deSec);
                            }
                            if (dateCompare(first.EXPIRATION_DATE, second.EXPIRATION_DATE) > 0) {
                                var deSec2 = deepCopy(first);
                                deSec2.EFFECTIVE_DATE = first.EXPIRATION_DATE;
                                deSec2.EXPIRATION_DATE = second.EXPIRATION_DATE;
                                var findCsrl2 = filterCsrl(CSRL, deSec2.ITEM_NBR, deSec2.PRICE_TRAIT, deSec2.EFFECTIVE_DATE, deSec2.EXPIRATION_DATE);
                                if (isNotEmpty(findCsrl2)) {
                                    findCsrl2.sort(function (a, b) {
                                        return a.CUST_BASE_RTL_AMT > b.CUST_BASE_RTL_AMT ? 1 : -1;
                                    });
                                    deSec2.RETAIL = findCsrl2[0].CUST_BASE_RTL_AMT;
                                    deSec2.TYPE = 'csrl';
                                }
                                firstList.push(deSec2);
                            }
                            var firstAll = firstList.concat(first);
                            var secondAll = secondList.concat(second);
                            newList.put(firQty, firstAll);
                            newList.put(secQty, secondAll);
                            if (calQty(newList, errorMap)) {
                                return true;
                            } else {
                                newList.clear();
                            }
                        } else {
                            var firstList2 = [];
                            var secondList2 = [];
                            //如果不相交则计算该箱包在csrl中的数据
                            var otherCsrl = filterCsrl(CSRL, second.ITEM_NBR, second.PRICE_TRAIT, first.EFFECTIVE_DATE, first.EXPIRATION_DATE);
                            var itemSecond;
                            for (var lis = 0; lis < itemSpilt.length; lis++) {
                                var itemI = itemSpilt[lis];
                                if (isNotEmpty(itemI.EFFECTIVE_DATE)
                                    && overlaps(first.EFFECTIVE_DATE, first.EXPIRATION_DATE, itemI.EFFECTIVE_DATE, itemI.EXPIRATION_DATE)
                                    && itemI.QTY !== first.QTY && (second.EFFECTIVE_DATE != itemI.EFFECTIVE_DATE && second.EXPIRATION_DATE != itemI.EFFECTIVE_DATE)) {
                                    itemSecond = itemI;
                                    break;
                                }
                            }
                            if (isNotEmpty(itemSecond)) {
                                secondList2.push(itemSecond);
                            } else if (isNotEmpty(otherCsrl)) {
                                var deSec3 = deepCopy(second);
                                otherCsrl.sort(function (a, b) {
                                    return a.CUST_BASE_RTL_AMT > b.CUST_BASE_RTL_AMT ? 1 : -1;
                                })
                                deSec3.EFFECTIVE_DATE = first.EFFECTIVE_DATE;
                                deSec3.EXPIRATION_DATE = second.EXPIRATION_DATE;
                                deSec3.RETAIL = otherCsrl[0].CUST_BASE_RTL_AMT;
                                deSec3.TYPE = 'csrl';
                                secondList2.push(deSec3)
                            }
                            firstList2.push(first);
                            var secondAll2 = secondList2.concat(second);
                            newList.put(firQty, firstList2);
                            newList.put(secQty, secondAll2);
                            if (calQty(newList, errorMap)) {
                                return true;
                            } else {
                                newList.clear();
                            }
                        }
                    }
                }
            }
        }
    }
}
